import 'package:enjoy_plus_01/utils/http.dart';
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
// import 'package:flutter_html/flutter_html.dart';
import 'package:toastification/toastification.dart';

class NoticeDetail extends StatefulWidget {
  const NoticeDetail({super.key});

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  Map<String, dynamic> params = {};
  Map notifyDetail = {};
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 获取路由传递的id
    final data = ModalRoute.of(context)?.settings.arguments;
    if (data != null) {
      // 根据id获取详情
      params = data as Map<String, dynamic>;
      getNotifyDetail();
    }
  }

  void getNotifyDetail() async {
    try {
      // 请求数据
      var res = await httpClass.request(
          requestParam:
              RequestParamModel(url: '/announcement/${params['id']}'));
      if (res['code'] != 10000)
        toastification.showError(context: context, title: '获取数据失败');
      setState(() {
        notifyDetail = res['data'];
        // 页面要展示的内容
        print(notifyDetail);
      });
    } catch (e) {
      toastification.showError(context: context, title: '请求失败：$e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('公告详情'),
        ),
        body: ListView(children: [
          Container(
              padding: const EdgeInsets.all(10),
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // 标题
                    Text(notifyDetail['title'] ?? '',
                        style: const TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        )),
                    const SizedBox(height: 10),
                    Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(notifyDetail['creatorName'] ?? '',
                              style: const TextStyle(color: Colors.grey)),
                          Text(notifyDetail['createdAt'] ?? '',
                              style: const TextStyle(color: Colors.grey))
                        ]),
                    const SizedBox(height: 10),
                    // 
                    HtmlWidget(notifyDetail['content'] ?? '')
                    //flutter_html: ^2.2.1这个包会报错，换成flutter_widget_from_html_core
                    // 内容
                    // Html(
                    //   data: notifyDetail['content'] ?? '',
                    // )
                  ]))
        ]));
  }
}
